var vm = new Vue({
    el: '#app',
    created: function () {
        var that = this;

        $.ajax({
            'url': '/trainPlatform/industryKeyword/search',
            'success': function (resp) {
                if (resp.success) {
                    that.data = resp.data;

                    for (var i = 0; i < that.data.length; i++) {
                        that.data[i]['managing'] = false;
                    }
                }
            }
        });
    },
    data: {
        data: []
    },
    computed: {},
    methods: {
        'add': function (i) {
            var that = this;

            mdui.prompt('新关键字(多个关键字可用斜杠分开)', '关键字', function (text) {
                    $.ajax({
                        'url': '/trainPlatform/industryKeyword/add',
                        'data': {
                            'name': that.data[i]['name'],
                            'kw': text
                        },
                        'success': function (resp) {
                            if (resp.success) {
                                mdui.snackbar({
                                    message: '插入成功'
                                });

                                for (var i = 0; i < text.split('/').length; i++) {
                                    that.data[i]['keywords'].push(text.split('/')[i]);
                                }
                            } else {
                                alert(resp.msg);
                            }
                        }
                    });
                }, function (text) {
                },
                {
                    'confirmText': '确定',
                    'cancelText': '取消',
                    'defaultValue': ''
                });
        },
        'manager': function (i) {
            if (this.data[i]['managing']) {
                Vue.set(this.data, i, {
                    'name': this.data[i]['name'],
                    'managing': false,
                    'keywords': this.data[i]['keywords']
                });
            } else {
                Vue.set(this.data, i, {
                    'name': this.data[i]['name'],
                    'managing': true,
                    'keywords': this.data[i]['keywords']
                });
            }
        },
        'del': function (industry_index, kw_index) {
            var that = this;

            var name = this.data[industry_index]['name'];
            var kw = this.data[industry_index]['keywords'][kw_index]

            $.ajax({
                'url': '/trainPlatform/industryKeyword/del',
                'data': {
                    'name': name,
                    'kw': kw
                },
                'success': function (resp) {
                    if (resp.success) {
                        that.data[industry_index]['keywords'].splice(kw_index, 1);
                    } else {
                        alert(resp.msg);
                    }
                }
            });
        },
        'searchInput': function () {

        },
        'importNewIndustry': function () {
            var that = this;

            mdui.prompt('新行业导入', '行业名称', function (text) {
                    $.ajax({
                        'url': '/trainPlatform/industryKeyword/newIndustry',
                        'data': {
                            'name': text
                        },
                        'success': function (resp) {
                            if (resp.success) {
                                mdui.snackbar({
                                    message: '成功新建一个行业'
                                });

                                that.data.push({
                                    'name': text,
                                    'keywords': [],
                                    'managing': false
                                });
                            } else {
                                alert(resp.msg);
                            }
                        }
                    });
                }, function (text) {
                },
                {
                    'confirmText': '确定',
                    'cancelText': '取消',
                    'defaultValue': ''
                });
        }
    },
    components: {}
});